package com.cetcs.kmga.dataManager.util;

import com.google.common.collect.Lists;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.Get;
import org.apache.hadoop.hbase.client.Result;
import org.apache.hadoop.hbase.client.Table;
import org.apache.hadoop.hbase.util.Bytes;

import java.util.List;
import java.util.Map;
import java.util.concurrent.Callable;

/**
 * 多线程获取数据
 *
 * @author xutao
 * @version V1.0 创建时间：2017-09-28 10:00
 *          Copyright 2017 by CETC
 */
public class HBaseDataGetter implements Callable<Result[]> {

    /**
     * rowKey的值
     */
    private List<String> rowKeys;
    /**
     * 表名
     */
    private String tbName;

    public HBaseDataGetter(String tbName, List<String> rowKeys) {
        this.rowKeys = rowKeys;
        this.tbName = tbName;
    }

    @Override
    public Result[] call() throws Exception {
        Table table = HBasePool.getInstance().getConnection().getTable(TableName.valueOf(tbName));
        List<Get> listGets = Lists.newArrayList();
        for (String rk : rowKeys) {
            Get get = new Get(Bytes.toBytes(rk));
            listGets.add(get);
        }
        Result[] resultArr = table.get(listGets);
        return resultArr;
    }
}
